#include "dir.h"

int init_session(session_t *session, char *username, MYSQL *mysql){
    bzero(session, sizeof(session_t));

    char query[MAX_QUERY_LEN] = {0}; 
    sprintf(query, "select id from file where username = '%s' and parent_id = -1", username);
    int ret = mysql_query(mysql, query);
    //if(ret != 0){
    //    fprintf(stderr, "%s:%s", "mysql_query", mysql_error(mysql));
    //    return -1;
    //}
    MYSQL_QUERY_CHECK(ret, mysql);

    MYSQL_RES *table = mysql_store_result(mysql);
    //if(table){
    //    fprintf(stderr, "%s:%s", "mysql_store_result", mysql_error(mysql));
    //    mysql_free_result(table);
    //    return -1;
    //}
    MYSQL_STORE_RESULT_CHECK(table, mysql);

    MYSQL_ROW row = mysql_fetch_row(table);
    session->curDirID = atoi(row[0]);
    //mysql_fetch_row函数从数据库中取出的任何数据 在C中为字符串

    strncpy(session->username, username, sizeof(session->username)-1);

    strncpy(session->curVirPath, "/", sizeof(session->curVirPath)-1);

    mysql_free_result(table);

    return 0;
}
